﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace CiteWords
{
    public partial class Form1 : Form
    {
        public List<Tuple<string, string>> wordlist = dbtool.get_tuple_list();
        public int index = 0;
        public Form1()
        {
            InitializeComponent();
            label1.Text = wordlist[index].Item1;
            label2.Text = wordlist[index].Item2;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //dbtool.LoadData();
            if (textBox1.Text == label1.Text) { label3.Text = "回答正确"; }
            else { label3.Text = "回答错误"; }
            index += 1;
            if (index >= wordlist.Count) { index = 0; }
            label1.Text = wordlist[index].Item1;
            label2.Text = wordlist[index].Item2;
            textBox1.Text = "";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (textBox1.Text == label1.Text) { label3.Text = "回答正确"; }
                else { label3.Text = "回答错误"; }
            }
        }
    }



    public class dbtool{
        
        static SqlConnection sql = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename ="+Path.GetFullPath("../../wc.mdf") +";Integrated Security=True;Connect Timeout=30");
        public dbtool()
        {
        }
        static public void LoadData()
        {
                 sql.Open();


                SqlCommand cmd = new SqlCommand("BEGIN TRANSACTION", sql);
                cmd.ExecuteNonQuery();

                string[] lines = File.ReadAllLines(@"./六级.txt", Encoding.UTF8);
            cmd = new SqlCommand("Create table wordTable(english nvarchar(MAX),chinese nvarchar(MAX))", sql);
            cmd.ExecuteNonQuery();
                string insert = "INSERT INTO wordTable(english,chinese) VALUES(@english,";
            int i = 0;
            int count = 0;
            while (i < lines.Length)
            {
                cmd = new SqlCommand(insert+ "N'" + lines[i + 1] + "');", sql);
                cmd.Parameters.Add(new SqlParameter("english", lines[i]));
                i += 2;
                count+=cmd.ExecuteNonQuery();
            }
                
                cmd = new SqlCommand("COMMIT", sql);
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                sql.Close();
            
        }

        static public List<Tuple<string, string>> get_tuple_list()
        {
            List<Tuple<string, string>> rsult = new List<Tuple<string, string>>();
            sql.Open();
            using (SqlCommand cmd = new SqlCommand("select * from wordTable; ",sql)) {
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    Tuple<string, string> t = new Tuple<string, string>(dr.GetString(0),dr.GetString(1));
                    rsult.Add(t);
                }
            }
            sql.Close();
            return rsult;
                
            
        }

}
}
